var addnewproject = (function(anp){
    var initials = false, cID= 0, pID=0;
    function RefreshService(gsID, action){
        dataBase.getServicesList(gsID, function(data){
            var i= 0, num = data.length, str=[];
            for(i;i<num;i+=1){
                str.push("<option value='" + data[i].ServicesID + "'>" + data[i].Services + "</option> ");
            }
            $("#selServicesProject").html(str.join('')).selectmenu('refresh', true);
            if(action)
                action();
        });
    }
    function ResetGui(){
        $("#txtProjectName").val("");
        $("#txtClientRef").val("");
        $("#txtPriceProject").val("");
        $("#txtVolumeProject").val("");
        $("#txtInstructions").val("");
        $("#txtWorkNotes").val("");
    };
    function FillData(){
        return {
            ProjectID:pID,
            ClientID:cID,
            Name:$("#txtProjectName").val(),
            ClientRef:$("#txtClientRef").val(),
            GroupsOfServicesID: parseInt($("#selGroupsOfServicesProject option:selected").val()),
            ServicesID: parseInt($("#selServicesProject option:selected").val()),
            VolumeUnitsID: parseInt($("#selVolumeUnitsProject option:selected").val()),
            Price: parseFloat($("#txtPriceProject").val()),
            ProjectVolume:$("#txtVolumeProject").val(),
            PriceType:$("#selPriceTypeProject option:selected").val(),
            AssignedDate:$("#txtAssignedDate").val(),
            DeadlineDate:$("#txtDeadlineDate").val(),
            CompletedDate:($("#chbCompleted").attr("checked")=="checked" ? $("#txtAssignedDate").val() : null),
            Completed:$("#chbCompleted").attr("checked")=="checked" ? 1 : 0,
            Specialization:$("#selSpecialization option:selected").val(),
            Instruction:$("#txtInstructions").val(),
            WorkNotes:$("#txtWorkNotes").val(),
            Priority:$("#selPriority option:selected").val(),
            Status:$("#selStatus option:selected").val()
        } ;

    };
    anp.initAddNewProject = function(clientID){
        cID = clientID;
        pID = 0;
        anp.init();
        var currentDate = new Date()
        var day = ("0" + currentDate.getDate()).slice(-2);;
        var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);;
        var year = currentDate.getFullYear();
        var my_date = year+"-"+month+"-"+day;
        $("#txtAssignedDate").val(my_date);
        $("#txtDeadlineDate").val(my_date);
    };
    anp.editAddNewContactPrice = function(contactPriceID, clientID){
        pID = contactPriceID;
        cID = clientID;
        anp.init();
        dataBase.getProjectByID(pID, function(data){
            $("#txtProjectName").val(data.Name);
            $("#txtClientRef").val(data.ClientRef);
            $("#selGroupsOfServicesProject option[value='" + data.GroupsOfServicesID + "']").attr('selected','selected');
            $("#selGroupsOfServicesProject").selectmenu('refresh', true);
            RefreshService(data.GroupsOfServicesID, function(){
                $("#selServicesProject option[value='" + data.ServicesID + "']").attr('selected','selected');
                $("#selServicesProject").selectmenu('refresh', true);
            });
            $("#selVolumeUnitsProject option[value='" + data.VolumeUnitsID + "']").attr('selected','selected');
            $("#selVolumeUnitsProject").selectmenu('refresh', true);
            $("#txtPriceProject").val(data.Price);
            $("#txtVolumeProject").val(data.Volume);
            $("#selPriceTypeProject option[value='" + data.PriceType + "']").attr('selected','selected');
            $("#selPriceTypeProject").selectmenu('refresh', true);
            $("#txtAssignedDate").val(data.AssignedDate);
            $("#txtDeadlineDate").val(data.DeadlineDate);
            if(data.Completed === true)
                $("#chbCompleted").attr('checked','checked');
            $("#selSpecialization option[value='" + data.Specialization + "']").attr('selected','selected');
            $("#selSpecialization").selectmenu('refresh', true);
            $("#selPriority option[value='" + data.Priority + "']").attr('selected','selected');
            $("#selPriority").selectmenu('refresh', true);
            $("#selStatus option[value='" + data.Status + "']").attr('selected','selected');
            $("#selStatus").selectmenu('refresh', true);
            $("#txtWorkNotes").val(data.WorkNotes);
            $("#txtInstructions").val(data.Instruction);
        });

    };
    anp.init = function(){
        if(!initials){
            $("#btnProjectSave").click(function (){
                if(pID === 0){
                    var newData = FillData();
                    dataBase.insertProject(newData,function (){
                        //pricesforclient.loadPricedForClient(cID);
                        ResetGui();
                        $.mobile.changePage("#pnClient");
                    });
                }else{
                    var newData = FillData();
                    dataBase.editProject(newData, function (clientID){
                        //pricesforclient.loadPricedForClient(cID);
                        ResetGui();
                        $.mobile.changePage("#pnClient");
                    });
                }
            });
            $("#btnProjectBack").click( function (){
                pricesforclient.loadPricedForClient(cID);
                $.mobile.changePage("#pnClient");
            });
            dataBase.getGroupsOfServicesList(function(data){
                var i= 0, num = data.length, str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].GroupsOfServicesID + "'>" + data[i].GroupName + "</option> ");
                }
                $("#selGroupsOfServicesProject").html(str.join('')).selectmenu('refresh', true);
                RefreshService(data[0].GroupsOfServicesID);
            });
            $("#selGroupsOfServicesProject").live("change", function() {
                RefreshService($("#selGroupsOfServicesProject option:selected").val());
            });
            dataBase.getVolumeUnitsList(function(data){
                var i= 0, num = data.length, str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].VolumeUnitsID + "'>" + data[i].Name + "</option> ");
                }
                $("#selVolumeUnitsProject").html(str.join('')).selectmenu('refresh', true);
            });
        }
        initials = true;
    };

    anp.init();

    return anp;
}(addnewproject || {}));
